<template>
  <PageWrapper dense>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          :preIcon="IconEnum.EXPORT"
          @click="
            downloadExcel((): Promise<Blob> => {
              return braceletExport(braceletQuery);
            }, '手环信息数据')
          "
          v-auth="'biz:bracelet:export'"
          >数据</a-button
        >
        <a-button
          :preIcon="IconEnum.DELETE"
          type="error"
          @click="multipleRemove(braceletRemove)"
          :disabled="!selected"
          v-auth="'biz:bracelet:remove'"
          >删除</a-button
        >
        <a-button
          type="primary"
          :pre-icon="IconEnum.ADD"
          @click="handleCreate"
          v-auth="'biz:bracelet:add'"
          >生成手环</a-button
        >
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '删除',
                icon: IconEnum.DELETE,
                type: 'primary',
                danger: true,
                ghost: true,
                auth: 'biz:bracelet:remove',
                popConfirm: {
                  placement: 'left',
                  title: '是否删除手环信息[' + record.id + ']?',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <BraceletModal @register="registerModal" @reload="reload" />
  </PageWrapper>
</template>

<script setup lang="ts">
  import { PageWrapper } from '@/components/Page';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { braceletList, braceletExport, braceletRemove } from '@/api/bracelet/manage';
  import { downloadExcel } from '@/utils/file/download';
  import { useModal } from '@/components/Modal';
  import BraceletModal from './BraceletModal.vue';
  import { formSchems, columns } from './bracelet.data';
  import { IconEnum } from '@/enums/appEnum';

  defineOptions({ name: 'Bracelet' });

  let braceletQuery = {};
  const [registerTable, { reload, multipleRemove, selected }] = useTable({
    rowSelection: {
      type: 'checkbox',
    },
    title: '手环信息列表',
    api: braceletList,
    showIndexColumn: false,
    rowKey: 'id',
    useSearchForm: true,
    formConfig: {
      schemas: formSchems,
      baseColProps: {
        span: 8,
      },
    },
    columns: columns,
    actionColumn: {
      width: 200,
      title: '操作',
      key: 'action',
      fixed: 'right',
    },
    handleSearchInfoFn: (e) => {
      braceletQuery = e;
    },
  });

  const [registerModal, { openModal }] = useModal();

  function handleCreate() {
    openModal(true, { update: false });
  }

  async function handleDelete(record: Recordable) {
    await braceletRemove([record.id]);
    await reload();
  }
</script>

<style scoped></style>
